﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.Linq;
using System.Data;
using System.Data.SqlClient;
using BaseLinQ.DataContex;
using Core;

namespace RBAC
{
    public class NguoiDungLogic
    {
        static string _conn = ConfigurationSettings.AppSettings["ConnectString"];
        RBACDataContext db = null;
        public List<tbl_NguoiDung> lstMenu = null;
        public List<tbl_NguoiDung> lstMenuTmp = null;

        public  DataTable GetListPaging(string strSort, string strDir, String strFilter, string intID)
        {
            DataTable dtbl = null;
            DataSet dts = null;
            List<object> lstNhatKyVaoRa = new List<object>();
            int intTotal = 0;
            try
            {
                SqlParameter[] commandParameter = null;
                commandParameter = new SqlParameter[4];

                commandParameter[0] = new SqlParameter();
                commandParameter[0].ParameterName = "@filter";
                commandParameter[0].Value = strFilter;

                commandParameter[1] = new SqlParameter();
                commandParameter[1].ParameterName = "@UserID";
                commandParameter[1].Value = intID;

                commandParameter[2] = new SqlParameter();
                commandParameter[2].ParameterName = "@sort";
                commandParameter[2].Value = strSort;

                commandParameter[3] = new SqlParameter();
                commandParameter[3].ParameterName = "@dir";
                commandParameter[3].Value = strDir;

                if (commandParameter != null)
                {
                    dts = SqlHelper.ExecuteDataset(_conn, "sp_NguoiDung_Select", commandParameter);
                    if (dts != null)
                    {
                        if (dts.Tables.Count > 0)
                        {
                            if (dts.Tables[0].Rows.Count > 0)
                            {
                                intTotal = int.Parse("0" + dts.Tables[0].Rows[0]["Total"].ToString());
                                dtbl = dts.Tables[0];
                                dts = null;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                }
            return dtbl;
        }

        /// <summary>
        /// Lay danh sach 
        /// </summary>
        /// <returns></returns>
        public List<tbl_NguoiDung> GetList()
        {
            List<tbl_NguoiDung> lstRes = null;
            db = new RBACDataContext(_conn);
            try
            {
                lstRes = (from m in db.tbl_NguoiDungs
                          where m.bKichHoat == true
                          select m).ToList<tbl_NguoiDung>();
            }
            catch (Exception ex)
            {
                
            }
            db = null;
            return lstRes;
        }

        public List<tbl_NguoiDung> GetListByDonViID(long _lngDonViID)
        {
            List<tbl_NguoiDung> lstRes = null;
            db = new RBACDataContext(_conn);
            try
            {
                lstRes = (from m in db.tbl_NguoiDungs
                          where m.bKichHoat == true
                          && m.fk_iDonViID == _lngDonViID
                          select m).ToList<tbl_NguoiDung>();
            }
            catch (Exception ex)
            {
               
            }
            db = null;
            return lstRes;
        }

        public List<tbl_NguoiDung> GetListByNguoiDungID(long _lngNguoiDungID)
        {
            List<tbl_NguoiDung> lstRes = null;
            db = new RBACDataContext(_conn);
            try
            {
                lstRes = (from m in db.tbl_NguoiDungs
                          where m.bKichHoat == true
                          && m.fk_iNguoiTaoID == _lngNguoiDungID
                          select m).ToList<tbl_NguoiDung>();
            }
            catch (Exception ex)
            {
               
            }
            db = null;
            return lstRes;
        }

        /// <summary>
        /// Lấy  theo ID
        /// </summary>
        /// <param name="_lngNguoiDungID"></param>
        /// <returns></returns>
        public tbl_NguoiDung GetByID(long _lngNguoiDungID)
        {
           tbl_NguoiDung objRes = null;
            db = new RBACDataContext(_conn);
            try
            {
                objRes = (from m in db.tbl_NguoiDungs
                          where m.bKichHoat == true
                          && m.pk_iNguoiDungID == _lngNguoiDungID
                          select m).FirstOrDefault<tbl_NguoiDung>();
            }
            catch (Exception ex)
            {
                
            }
            return objRes;
        }

        /// <summary>
        /// Lấy thao mã
        /// </summary>
        /// <param name="_strTenDangNhap"></param>
        /// <returns></returns>
        public tbl_NguoiDung CheckUserLogin(string _strTenDangNhap, string _strPassword)
        {
            tbl_NguoiDung objRes = null;
            db = new RBACDataContext(_conn);
            try
            {
                objRes = (from m in db.tbl_NguoiDungs
                          where m.bKichHoat == true
                          && m.sTenDangNhap == _strTenDangNhap
                          && ((m.sMatKhauMoi != null && m.sMatKhauMoi == _strPassword) || m.sMatKhau == _strPassword)
                          select m).FirstOrDefault<tbl_NguoiDung>();
                
                //neu lan dau tien dang nhap bang mat khau moi thi cap nhat lai
                if (objRes.sMatKhauMoi != null)
                {
                    objRes.sMatKhau = _strPassword;
                    objRes.sMatKhauMoi = null;
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                
            }
            return objRes;
        }
        public tbl_NguoiDung GetByCode(string _strTenDangNhap)
        {
            tbl_NguoiDung objRes = null;
            db = new RBACDataContext(_conn);
            try
            {
                objRes = (from m in db.tbl_NguoiDungs
                          where m.bKichHoat == true
                          && m.sTenDangNhap == _strTenDangNhap
                          select m).FirstOrDefault<tbl_NguoiDung>();
            }
            catch (Exception ex)
            {
                
            }
            return objRes;
        }
        public tbl_NguoiDung checkUser(string _strTenDangNhap, long _ID)
        {
            tbl_NguoiDung objRes = null;
            db = new RBACDataContext(_conn);
            try
            {
                objRes = (from m in db.tbl_NguoiDungs
                          where m.bKichHoat == true
                          && m.pk_iNguoiDungID != _ID
                          && m.sTenDangNhap == _strTenDangNhap
                          select m).FirstOrDefault<tbl_NguoiDung>();
            }
            catch (Exception ex)
            {
                
            }
            return objRes;
        }

        /// <summary>
        /// Them moi
        /// </summary>
        /// <param name="_objNguoiDung"></param>
        /// <returns></returns>
        public bool Insert(tbl_NguoiDung _objNguoiDung)
        {
            bool blnFalse = false;
            db = new RBACDataContext(_conn);
            try
            {
                _objNguoiDung.dNgayTao = DateTime.Now;
                _objNguoiDung.dNgayCapNhat = DateTime.Now;
                db.tbl_NguoiDungs.InsertOnSubmit(_objNguoiDung);
                db.SubmitChanges();
                blnFalse = true;
            }
            catch (Exception ex)
            {
               
            }
            db = null;
            return blnFalse;
        }

        /// <summary>
        /// Cap nhat thong tin 
        /// </summary>
        /// <param name="_objNguoiDungNew"></param>
        /// <returns></returns>
        public bool Update(tbl_NguoiDung _objNguoiDungNew)
        {
            bool blnRes = false;
            db = new RBACDataContext(_conn);
            try
            {
                tbl_NguoiDung objNguoiDungOld = (from m in db.tbl_NguoiDungs
                                       where m.pk_iNguoiDungID == _objNguoiDungNew.pk_iNguoiDungID
                                                 select m).FirstOrDefault<tbl_NguoiDung>();
                if (objNguoiDungOld != null && objNguoiDungOld.pk_iNguoiDungID > 0)
                {
                    objNguoiDungOld.sEmail = _objNguoiDungNew.sEmail;
                    objNguoiDungOld.sGhiChu = _objNguoiDungNew.sGhiChu;
                    objNguoiDungOld.sSoDienThoai = _objNguoiDungNew.sSoDienThoai;
                    objNguoiDungOld.sHoTen = _objNguoiDungNew.sHoTen;
                    objNguoiDungOld.sTenDangNhap = _objNguoiDungNew.sTenDangNhap;
                    objNguoiDungOld.fk_iDonViID = _objNguoiDungNew.fk_iDonViID;
                    objNguoiDungOld.fk_iNguoiCapNhatID = _objNguoiDungNew.fk_iNguoiCapNhatID;
                    objNguoiDungOld.dNgayCapNhat = DateTime.Now;
                    if (_objNguoiDungNew.sPath != null && _objNguoiDungNew.sPath != "")
                    {
                        objNguoiDungOld.sPath = _objNguoiDungNew.sPath;
                    }
                    db.SubmitChanges();
                    blnRes = true;
                }
            }
            catch (Exception ex)
            {
                
            }
            db = null;
            return blnRes;
        }

        /// <summary>
        /// Xoa 
        /// </summary>
        /// <param name="_pk_iNguoiDungID"></param>
        /// <returns></returns>
        public bool Delete(long _pk_iNguoiDungID)
        {
            bool blnRes = false;
            db = new RBACDataContext(_conn);
            try
            {
                //Lấy thông tin người dùng cần xóa
                tbl_NguoiDung objNguoiDungOld = (from m in db.tbl_NguoiDungs
                                       where m.pk_iNguoiDungID == _pk_iNguoiDungID
                                                 select m).FirstOrDefault<tbl_NguoiDung>();
                //Lấy list người dùng bị xóa
                List<tbl_NguoiDung> lstNguoiDungDel = (from n in db.tbl_NguoiDungs
                                                       where n.sPath.Contains(objNguoiDungOld.sPath)
                                                       && n.bKichHoat == true
                                                       select n).ToList<tbl_NguoiDung>();
                foreach (tbl_NguoiDung nguoidungold in lstNguoiDungDel)
                {
                    //Tìm rằng buộc trong bảng VaiTroNguoiDungDonVi
                    List<tbl_VaiTroNguoiDungDonVi> LstVaiTroNguoiDungDonVi = (from a in db.tbl_VaiTroNguoiDungDonVis
                                                                              where a.fk_iNguoiDungID == nguoidungold.pk_iNguoiDungID
                                                                              && a.bKichHoat == true
                                                                              select a).ToList<tbl_VaiTroNguoiDungDonVi>();
                    List<tbl_NguoiDungQuyenDonVi> lstNDQDV = (from b in db.tbl_NguoiDungQuyenDonVis
                                                              where b.fk_iNguoiDungID == nguoidungold.pk_iNguoiDungID
                                                              select b).ToList<tbl_NguoiDungQuyenDonVi>();
                    if (nguoidungold != null && nguoidungold.pk_iNguoiDungID > 0)
                    {
                        nguoidungold.bKichHoat = false;
                        ////Xóa rằng buộc

                        if (LstVaiTroNguoiDungDonVi != null)
                        {
                            foreach (tbl_VaiTroNguoiDungDonVi objVTNDDV in LstVaiTroNguoiDungDonVi)
                            {
                                objVTNDDV.bKichHoat = false;
                            }
                        }
                        if (lstNDQDV != null && lstNDQDV.Count > 0)
                        {
                            db.tbl_NguoiDungQuyenDonVis.DeleteAllOnSubmit(lstNDQDV);
                        }
                        db.SubmitChanges();
                    }
                }
                blnRes = true;
            }
            catch (Exception ex)
            {
                blnRes = false;
               
            }
            db = null;
            return blnRes;
        }

        /// <summary>
        /// Lấy danh sách các đơn vị gốc
        /// </summary>
        /// <returns></returns>
        public List<tbl_Donvi> GetListOrgRoot()
        {
            List<tbl_Donvi> lstRes = null;
            RBACDataContext db = new RBACDataContext(_conn);
            try
            {
                tbl_Donvi objOrgRoot = (from dv in db.tbl_Donvis
                                        where dv.iParent_ID == null
                                        && dv.Status == true
                                        select dv).FirstOrDefault<tbl_Donvi>();
                if(objOrgRoot != null && objOrgRoot.pk_iDonViID > 0)
                {
                    lstRes = (from dv in db.tbl_Donvis
                              where dv.iParent_ID == objOrgRoot.pk_iDonViID
                              && dv.Status == true
                              select dv).ToList<tbl_Donvi>();
                }
            }
            catch (Exception ex)
            {
                
            }
            db = null;
            return lstRes;
        }

        public bool UpdateSomeInfor(tbl_NguoiDung _UserNew)
        {
            RBACDataContext db = new RBACDataContext(_conn);
            bool bRes = false;
            try
            {
                tbl_NguoiDung objOld = (from u in db.tbl_NguoiDungs
                                        where u.pk_iNguoiDungID == _UserNew.pk_iNguoiDungID
                                        select u).FirstOrDefault<tbl_NguoiDung>();
                db.SubmitChanges();
                bRes = true;
            }
            catch (Exception ex)
            {
                
            }
            db = null;
            return bRes;
        }

        public bool CheckPass(tbl_NguoiDung _User)
        {
            RBACDataContext db = new RBACDataContext(_conn);
            bool bRes = false;
            try
            {
                tbl_NguoiDung objOld = (from u in db.tbl_NguoiDungs
                                        where u.pk_iNguoiDungID == _User.pk_iNguoiDungID
                                        select u).FirstOrDefault<tbl_NguoiDung>();

                if (objOld.sMatKhau == _User.sMatKhau)
                {
                    bRes = true;
                }
            }
            catch (Exception ex)
            {
                
            }
            db = null;
            return bRes;
        }

        public bool UpdatePassword(tbl_NguoiDung _UserNew)
        {
            RBACDataContext db = new RBACDataContext(_conn);
            bool bRes = false;
            try
            {
                tbl_NguoiDung objOld = (from u in db.tbl_NguoiDungs
                                        where u.pk_iNguoiDungID == _UserNew.pk_iNguoiDungID
                                        select u).FirstOrDefault<tbl_NguoiDung>();
                objOld.sMatKhau = _UserNew.sMatKhau;
                objOld.sMatKhauMoi = _UserNew.sMatKhauMoi;
                db.SubmitChanges();
                bRes = true;
            }
            catch (Exception ex)
            {
                
            }
            db = null;
            return bRes;
        }
    }
}
